#include "PreGraphic.h"
#include "Entity.h"
#include "Mesh.h"
#include "Material.h"
#include "RenderSystem.h"

Entity::Entity(void):mMesh(NULL),mMaterial(NULL)
{
}

Entity::~Entity(void)
{
	if(mMesh)
		delete mMesh;
	if(mMaterial)
		delete mMaterial;
}

void Entity::draw(RenderSystem* rs)
{
	//rs->setWorldMatrix(getWorldMatrix());

	D3DXMATRIX matrix;
	D3DXMatrixTranslation(&matrix,getPosition().x,getPosition().y,getPosition().z);
	rs->getD3DDevice()->SetTransform(D3DTS_WORLD,&matrix);

	if(mMaterial)
		mMaterial->setMaterial(rs);
	

	if(mMesh)
		mMesh->draw(rs->getD3DDevice());

	if(mMaterial){
		mMaterial->unsetMaterial(rs);
	}
}

bool Entity::loadFromXFile(const String & filename,RenderSystem* rs)
{
	if(mMesh){
		delete mMesh;
		mMesh = NULL;
	}
	mMesh = new StaticMesh;
	return mMesh->loadFromXFile(filename,rs->getD3DDevice());
}

void Entity::setMaterial(Material * mtrl)
{
	if(mMaterial){
		delete mMaterial;
		mMaterial = NULL;
	}
	mMaterial = mtrl;
}